package com.jilian.mydemo.modul.main.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.common.dto.BannerDto;
import com.jilian.mydemo.listener.ViewPagerItemClickListener;

import java.util.List;

/**
 * viewpager公共适配器
 *
 * @author weishixiong
 * @Time 2018-04-25
 */

public class GoodBananerPagerAdapter extends PagerAdapter {
    private List<String> list;
    private ViewPagerItemClickListener listener;
    private Context context;
    public GoodBananerPagerAdapter(Context context, List<String> list, ViewPagerItemClickListener listener) {
        this.list = list;
        this.listener = listener;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 :list.size();
    }


    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        String url = list.get(position);
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(context).load(url).into(imageView);
        container.addView(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onViewPageItemClick(view, position % list.size());
            }
        });
        return imageView;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
}
